package ljl.alg.jianzhioffer.round2;

import java.util.LinkedList;

public class _30_min_stack {
    
    static class MinStack {
        
        LinkedList<Integer> stack = new LinkedList<>();
        LinkedList<Integer> minStack = new LinkedList<>();
        
        /**
         * 还是初级程序员，这块判空都没写
         * */
        public void push(int x) {
            stack.push(x);
            int newMin = x;
            if (!minStack.isEmpty() && minStack.peek() < x) {
                newMin = minStack.peek();
            }
            minStack.push(newMin);
        }
        
        public void pop() {
            stack.pop();
            minStack.pop();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int min() {
            return minStack.peek();
        }
    }
    
    /**
     * 路飞总是技高一筹！
     * */
    static class MinStack3 {
        
        LinkedList<Integer> stack = new LinkedList<>();
        LinkedList<Integer> minStack = new LinkedList<>();
        
        public void push(int x) {
            stack.push(x);
            if (minStack.isEmpty() || minStack.peek() >= x) {
                minStack.push(x);
            }
        }
        
        public void pop() {
           if (stack.pop().equals(minStack.peek()))
               minStack.pop();
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int min() {
            return minStack.peek();
        }
    }
    
}
